package com.longcheer.weibo;

import com.longcheer.weibo.R;
import com.longcheer.weibo.api.StatusesApi;
import com.longcheer.weibo.vo.Status;
import com.weibo.net.WeiboException;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.List;

public class HomeActivity extends Activity {
    
    private static final String TAG = "HomeActivity";

    private ImageView mPublish;
    private TextView mNick;
    private ImageView mRefersh;
    private ListView mList;
    private ProgressBar mProgress;
    
    private WeiboListAdapter mAdapter;
    
    private static final int MSG_REFRESH_BEGIN = 0;
    private static final int MSG_REFRESH_END = 1;
    private static final int MSG_MORE_BEGIN = 3;
    private static final int MSG_MORE_END = 4;
    private static final int MSG_WEIBO_DETAIL = 5;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.home);
        
        mPublish = (ImageView)findViewById(R.id.iv_home_publish);
        mRefersh = (ImageView)findViewById(R.id.iv_home_refresh);
        mNick = (TextView)findViewById(R.id.tv_home_nick);
        mList = (ListView)findViewById(R.id.lv_home);
        
        mAdapter = new WeiboListAdapter(this);
        mList.setAdapter(mAdapter);
        
        mRefersh.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandler.sendEmptyMessage(MSG_REFRESH_BEGIN);
                mList.setSelection(0);
            }
        });
        
        mPublish.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startPublish();
            }
        });
        
        mList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mAdapter.getItemViewType(position) == WeiboListAdapter.VIEW_TYPE_MORE) {
                    mHandler.sendEmptyMessage(MSG_MORE_BEGIN);
                } else if (mAdapter.getItemViewType(position) == WeiboListAdapter.VIEW_TYPE_WEIBO) {
                    Status status = (Status)mAdapter.getItem(position);
                    startDetailWeibo(status);
                }
            }
        });
    }
    
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.d(TAG, "Call onRestoreInstanceState() method.");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Log.d(TAG, "Call onSaveInstanceState() method.");
        super.onSaveInstanceState(outState);
    }



    private void startDetailWeibo(Status status) {
        Intent intent = new Intent().setClass(this, DetailWeiboActivity.class);
        intent.putExtra("status", status);
        startActivity(intent);
    }
    
    private void startPublish() {
        Intent intent = new Intent().setClass(this, PublishActivity.class);
        startActivity(intent);
    }
    
    private void loadMoreWeibo() {
        long maxId = -1;
        List<Status> old = mAdapter.getStatusList();
        if (old.size() > 0) {
            maxId = old.get(old.size()-1).getId();
        } 
        
        try {
            List<Status> statuses = StatusesApi.getFriendsTimeline(this, maxId, 20);
            if (statuses != null) {
                mAdapter.addStatusList(statuses);
            }
        } catch (WeiboException e) {
            e.printStackTrace();
        }
        mHandler.sendEmptyMessage(MSG_MORE_END);
    }
    
    private void refreshWeibo() {
        try {
            List<Status> newStatuses = StatusesApi.getFriendsTimeline(this, 20);
            if (newStatuses.size() > 0) {
                mAdapter.setStatusList(newStatuses);
                mAdapter.notifyDataSetChanged();
            }
        } catch (WeiboException e) {
            e.printStackTrace();
        }
        mHandler.sendEmptyMessage(MSG_REFRESH_END);
    }
    
    private Handler mHandler = new Handler() {
        
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_REFRESH_BEGIN:
                refreshWeibo();
                break;
            case MSG_REFRESH_END:
                mAdapter.notifyDataSetChanged();
                break;
            case MSG_MORE_BEGIN:
                loadMoreWeibo();
                break;
            case MSG_MORE_END:
                mAdapter.notifyDataSetChanged();
                break;
            }
        }
        
    };
    
    @Override
    public void onBackPressed() {
        new ExitConfirmDialog(this).show();
    }
}
